/*
 * \file TitledDataModel.h
 *
 * \date Created on: Jan 31, 2009
 * \author sarah
 */

#ifndef TITLEDDATAMODEL_H_
#define TITLEDDATAMODEL_H_

#include <QAbstractItemModel>
#include <QStringList>
#include <QList>
#include <QIcon>

#include <DndData/TitledData.h>

class TitledData;

class TitledDataModel: public QAbstractItemModel
{
public:
    TitledDataModel(const TitledData *source, QObject *parent = 0);
    virtual ~TitledDataModel();
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &index) const;
    bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount (const QModelIndex &parent = QModelIndex()) const;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
protected:
    void clear();
    void addCategory(const QString &categoryName);
    void addEntry(int category, const QString &entryDetails);
    virtual QVariant data(int category, int entry, int role) const;
private:
    const TitledData *mSource;
    QStringList mCategoryNames;
    QList< QList <QVariant> > mDetails;
    QIcon mIcon;
};

namespace TitledDataUtil
{
    inline qint64 modelSpecToId(int category, int entry)
    {
        return (category << 16) | (0x0000ffff & entry);
    }

    inline int idToCategory(qint64 id)
    {
        return id >> 16;
    }

    inline int idToEntry(qint64 id)
    {
        return int(id) << 16 >> 16;
    }
};

#endif /* TITLEDDATAMODEL_H_ */
